package com.monkeygroup.mygoplus.view.home.activity.person;

import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.common.utils.LocalInfo;
import com.monkeygroup.mygoplus.common.utils.VilidateUtil;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.user.vo.PersonalNomalInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by cumt on 2017/7/13.
 */

public class PersonChangeEmailActivity extends BaseActivity implements View.OnClickListener {

    public static void gotoActivity(Activity activity) {
        Intent intent = new Intent(activity,
                PersonChangeEmailActivity.class);

        activity.startActivityForResult(intent,400);
    }

    private Activity activity;

    private ImageView imgBack;
    private TextView txtTitle;

    private EditText et_new_email;
    private TextView btn_change_eamil_ok;

    private LinearLayout ll_change_content,ll_input_content;

    //請求參數
    private Map<String, String> params;
    private HttpModel httpModel;

    @Override
    public void onClick(View v) {

    }

    @Override
    public void setContentView() {
        setContentView(R.layout.person_change_email);

    }

    @Override
    public void initView() {

        activity= PersonChangeEmailActivity.this;

        imgBack = (ImageView) findViewById(R.id.img_back);
        txtTitle = (TextView) findViewById(R.id.txt_title);
        txtTitle.setText("变更電子信箱");

        et_new_email= (EditText) findViewById(R.id.et_new_email);
        btn_change_eamil_ok= (TextView) findViewById(R.id.et_new_email);

        ll_input_content= (LinearLayout) findViewById(R.id.ll_input_content);
        ll_change_content= (LinearLayout) findViewById(R.id.ll_change_content);

        params = new HashMap<>();

        PersonalNomalInfo user = LocalInfo.getUserInfo(activity);
        params.put("mobile",user.getMobile());

        TextWatcher cname=new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if(VilidateUtil.isEmail(String.valueOf(et_new_email.getText()))){
                    params.put("email",String.valueOf(et_new_email.getText()));
                }
                else{
                    Toast.makeText(activity, "請輸入正確的郵箱",
                            Toast.LENGTH_SHORT).show();
                }


            }

            @Override
            public void afterTextChanged(Editable s) {



            }
        };

        et_new_email.addTextChangedListener(cname);
        initRequest();

    }

    @Override
    public void setListener() {

    }

    @Override
    public void doEvent() {

        btn_change_eamil_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(VilidateUtil.isEmail(String.valueOf(et_new_email.getText()))){
                    httpModel.startRequest();
                }
                else{
                    Toast.makeText(activity, "請輸入正確的郵箱",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private void initRequest(){
        httpModel = new HttpModel(RequestUrl.baseUrl+RequestUrl.updateUserUrl,params, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {

                if(VilidateUtil.isSuccess(resultCode)){

                    ll_input_content.setVisibility(View.GONE);
                    ll_change_content.setVisibility(View.VISIBLE);
                    Toast.makeText(activity, "修改成功",
                            Toast.LENGTH_SHORT).show();
                    final Intent intent=new Intent();
                    intent.putExtra("email", String.valueOf(params.get("email")));

                    activity.setResult(RESULT_OK,intent);
                    finish();

                }
                else{
                    Toast.makeText(activity, resultMsg+",修改失敗",
                            Toast.LENGTH_SHORT).show();
                }

            }
        },true);


    }



}
